<?php
$form = $this->beginWidget('PsActiveForm', array(
	'id' => 'add-receipt-form',
	'enableAjaxValidation' => false,
	'enableClientValidation' => true,
	'clientOptions' => array(
		'validationDelay' => 200,
		'inputContainer' => 'tr',
		'validateOnSubmit' => true,
		'validateOnChange' => false,
	)
		));
?>
<script type="text/javascript">
	var currency_code = "<?php echo (isset($receipt->currency_code) ? $receipt->currency_code : '$'); ?>";
</script>
<div id="addReceipt" class="">			
	<div class="grid_9">
		<div class="section">
			<div class="block">
				<h2>Receipt Information</h2>
				<div class="table-block">
					<table class="table">
						<tr>
							<th scope="row" width="120"><?php echo $form->labelEx($receipt, 'payer_name'); ?></th>
							<td>
								<?php if (!$receipt->isNewRecord): ?>
									<?php echo $form->textField($receipt, 'payer_name', array('readonly' => 'readonly')); ?>
								<?php else: ?>
									<?php
									$this->widget('common.widgets.PsJuiCategoryAutoComplete', array(
										'model' => $receipt,
										'attribute' => 'payer_name',
										'source' => $this->createUrl('/ajax/party/autocomplete'),
										// additional javascript options for the autocomplete plugin
										'options' => array(
											'showAnim' => 'fold',
											'select' => 'js:prestige.receipt.update.onPartySelected'
										),
										'htmlOptions' => array(
											'size' => '30',
											'placeholder' => 'Search contacts...'
										)
									));
									?>
									<button id="btAddContact" class="link-button with-icon" type="button">
										<img src="/images/icons/fugue/card--plus.png" />
										Add Contact
									</button>
								<?php endif; ?>

								<?php echo $form->hiddenField($receipt, 'payer_party_id'); ?>
								<?php echo $form->hiddenField($receipt, 'contact_id'); ?>
								<?php echo $form->error($receipt, 'payer_name', false); ?>
							</td>
						</tr>
						<tr>
							<th scope="row" width="120">
								<?php echo $form->labelEx($receipt, 'amount', array('label' => 'Amount Received')); ?>
							</th>
							<td>
								<?php
								$this->widget('common.widgets.PsAutoNumeric.PsAutoNumeric', array(
									'model' => $receipt,
									'attribute' => 'amount',
									'htmlOptions' => array(
										'class' => 'align-right',
										'size' => '20',
										'readonlhy' => 'readonly'
									),
									'options' => array(
										'aSign' => $receipt->currency_code . ' '
									),
								));
								?>
								<?php echo $form->error($receipt, 'amount'); ?>
							</td>
						</tr>
						<tr>
							<th scope="row" width="120">
								<?php echo $form->labelEx($receipt, 'payment_method_type_code', array('label' => 'Payment Method')); ?>
							</th>
							<td>
								<?php
								echo $form->dropDownList($receipt, 'payment_method_type_code', PaymentMethodType::getListData(), array(
									'prompt' => '[Select]',
								));
								?>
								<?php echo $form->textField($receipt, 'payment_method_other', array('style' => $receipt->payment_method_type_code == 'Other' ? '' : 'display: none;')); ?>
								<?php echo $form->error($receipt, 'payment_method_type_code'); ?>
							</td>
						</tr>
						<tr>
							<th scope="row" width="120">
								<?php echo $form->labelEx($receipt, 'payment_ref_no'); ?>
							</th>
							<td>
								<?php echo $form->textField($receipt, 'payment_ref_no'); ?>
								<?php echo $form->error($receipt, 'payment_ref_no'); ?>
							</td>
						</tr>
						<tr>
							<th scope="row" width="120">
								<?php echo $form->labelEx($receipt, 'comment'); ?>
							</th>
							<td>
								<?php echo $form->textField($receipt, 'comment'); ?>
								<?php echo $form->error($receipt, 'comment'); ?>
							</td>
						</tr>
					</table>
				</div>
			</div>
		</div>
	</div>
	<div class="grid_7">
		<div class="section">
			<div class="block">

				<table class="table">
					<tr>
						<th scope="row" width="120">
							<?php echo $form->labelEx($receipt, 'payment_number', array('label' => 'Receipt #')); ?>
						</th>
						<td>
							<?php echo $form->textField($receipt, 'payment_number', array('readonly' => ($this->action->id != 'create')?'readonly':'')); ?>
							<?php if($this->action->id == 'create'): ?>
							<?php echo $form->checkBox($receipt, 'automatic_numbering', array('uncheckValue' => 0)); ?>
							<?php
							echo $form->labelEx($receipt, 'automatic_numbering', array(
								'class' => 'tooltip-top',
								'title' => 'Automatically assign the next available number if the receipt number is taken',
								'label' => 'Automatic'
							));
							?>
							<?php endif; ?>
							<?php echo $form->error($receipt, 'payment_number'); ?>
						</td>
					</tr>
					<tr>
						<th scope="row" width="120">
							<?php echo $form->labelEx($receipt, 'payment_date', array('label' => 'Receipt Date')); ?>
						</th>
						<td>
							<?php
							$form->widget('PsJuiDatePicker', array(
								'model' => $receipt,
								'attribute' => 'payment_date',
								'options' => array(
									'showButtonPanel' => false,
									'changeYear' => true,
									'dateFormat' => 'dd-mm-yy',
								),
							));
							?>
							<?php echo $form->error($receipt, 'payment_date'); ?>
						</td>
					</tr>
					<tr>
						<th scope="row" width="120"><?php echo $form->labelEx($receipt, 'currency_code'); ?></th>
						<td>
							<?php
							echo $form->dropDownList($receipt, 'currency_code', array('SGD' => 'SGD - Singapore Dollar'));
							?>
							<?php echo $form->error($receipt, 'currency_code', false); ?>
						</td>
					</tr>
				</table>
			</div>
		</div>
	</div>
	<div id="invoices" class="grid_16">
		<div class="section">
			<div class="block">
				<h2>Open Invoices</h2>
				<?php
				$this->widget('PsGridView', array(
					'id' => 'payment_application_grid',
					'dataProvider' => $dataProvider,
					'template' => '{pager}{items}',
					'pagesizeCssClass' => 'pager-pagesize',
					'itemsCssClass' => 'items clear',
					'emptyText' => 'No invoices found.',
					'selectableRows' => 2,
					'htmlOptions' => array(
						'class' => 'grid-view grey',
					),
					'afterAjaxUpdate' => 'prestige.receipt.update.onGridViewUpdate',
					'columns' => array(
						'invoice_date' => array(
							'name' => 'invoice_date',
							'type' => 'raw',
							'value' => 'Yii::app()->dateFormatter->formatDateTime(strtotime($data->invoice_date), "short", null)',
							'headerHtmlOptions' => array(
								'class' => 'invoicecol-date',
							),
							'htmlOptions' => array(
								'width' => 80,
								'class' => 'invoicecol-date',
							),
						),
						'invoice_number' => array(
							'header' => 'Invoice #',
							'name' => 'invoice_number',
							'type' => 'raw',
							'value' => 'CHtml::link("#" . $data->invoice_number, array("invoice/view", "id" => $data->id), array("class" => "blue link"))',
							'headerHtmlOptions' => array(
								'class' => 'invoicecol-number',
							),
						),
						'total' => array(
							'header' => 'Total',
							'name' => 'total',
							'type' => 'raw',
							'value' => 'Yii::app()->numberFormatter->formatCurrency($data->getInvoiceTotal(), $data->currency_code)',
							'headerHtmlOptions' => array(
								'class' => 'invoicecol-total',
							),
							'htmlOptions' => array(
								'class' => 'invoicecol-total',
								'width' => 80,
							),
						),
						'total_due' => array(
							'header' => 'Amount Due',
							'type' => 'raw',
							'value' => 'Yii::app()->numberFormatter->formatCurrency($data->getBalance() + ' . ($receipt->isNewRecord ? '0.00' : '$data->getAmountPaidByPaymentId(' . $receipt->id . ')') . ', $data->currency_code) . CHtml::hiddenField("invoice_" . $data->id, $data->getBalance() + ' . ($receipt->isNewRecord ? '0.00' : '$data->getAmountPaidByPaymentId(' . $receipt->id . ')') . ', array("class" => "invoice_balance"))',
							'headerHtmlOptions' => array(
								'class' => 'invoicecol-amount-due',
							),
							'htmlOptions' => array(
								'class' => 'invoicecol-amount-due',
								'width' => 80,
							),
						),
						'amount_applied' => array(
							'header' => 'Amount Applied',
							'type' => 'raw',
							'value' => 'CHtml::textField("amount_applied[" . $data->id . "]", ' . ($receipt->isNewRecord ? ($invoice_id ? '$data->getBalance()' : '0.00') : '$data->getAmountPaidByPaymentId(' . $receipt->id . ')') . ', array("class" => "amount_applied"))',
							'headerHtmlOptions' => array(
								'class' => 'invoicecol-amount-applied',
							),
							'htmlOptions' => array(
								'class' => 'invoicecol-amount-applied',
							),
						),
						'id' => array(
							'class' => 'CCheckBoxColumn',
							'name' => 'id',
							'selectableRows' => 2,
							'checked' => ($invoice_id ? '$data->id == "' . $invoice_id . '"' : ''),
							'headerHtmlOptions' => array(
								'class' => 'invoicecol-check',
								'width' => 16,
							),
							'htmlOptions' => array(
								'class' => 'invoicecol-check',
							),
						),
					)
				));
				?>
			</div>
		</div>
	</div>
</div>
<?php $this->endWidget(); ?>
<div class="grid_16 relative">
	<button type="submit" class="link-button green no-margin with-icon" id="btnFinish">
		<span>Save Receipt</span>
	</button>
	&nbsp;or <a class="link" href="<?php echo $this->createUrl('receipt/index'); ?>">Cancel</a>
</div>